package com.younion.action.consumer.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IAppInitService;
import com.younion.common.constants.SysConstants;
import com.younion.model.AppInitResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.AppInitVO;

public class AppInitConsumer extends AbstractRequestConsumer {
	private IAppInitService appInitService = null;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getAppInit(request, response);
	}

	public String getAppInit(HttpServletRequest request,
			HttpServletResponse response) {
		AppInitResult appInitResult = new AppInitResult();
		List<AppInitVO> appInitVOList = null;
		ResultObject resultObject = new ResultObject();
		String client_type = request.getParameter("client_type");
		try {
			String resolution = ApiConstant.ANDROID_APP_INIT_RESOLUTION;
			if ("iphone".equals(client_type)) {
				resolution = ApiConstant.IPHONE_APP_INIT_RESOLUTION;
			}
			appInitVOList = (List<AppInitVO>) appInitService.getAll();
			if (appInitVOList != null && appInitVOList.size() > 0) {
				String img_url_pre = ConfigHelper.fastGetConfig("interface",
						"img_url_pre", "http://localhost:8080/younonmember/");
				AppInitVO appInitVO = appInitVOList.get(0);
				String[] pics = appInitVO.getPic().split(",");
				for (int i = 0; i < pics.length; i++) {
					if (pics[i].indexOf(resolution) >= 0) {
						appInitResult.setPic(img_url_pre
								+ SysConstants.APP_INIT_IMG_URL + "/"
								+ appInitVO.getId() + "/" + pics[i]);
					}
				}
				appInitResult.setAreaid(appInitVO.getAreaid().toString());
				appInitResult.setPicid(appInitVO.getPicid().toString());
			}
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		resultObject.setResult(appInitResult);
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setAppInitService(IAppInitService appInitService) {
		this.appInitService = appInitService;
	}

}
